/*
 * @Author: HanYaJun
 * @Date: 2023-01-14 10:09:40
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 星消灭-等级和目标分
 */

import { guiForm } from '../../../Public/game/guiManager/UIFrame/BaseForm';
import { GTR } from '../../../Public/GTR';
import LocalData from '../../../Public/game/gameData/LocalData';
import { LocalCacheDataEnum, LevelProgressEumn } from '../Global/xxmDefaultPlayerData';
import xxmGameEvents from '../Global/xxmGameEvents';
import xxmUIConfig from '../Global/xxmUIConfig';

let trace = function (...args) {
    GTR.logg.log("xxmLevelObjective【HYJ】", ...args);
}
let traceError = function (...args) {
    GTR.logg.error("xxmLevelObjective【HYJ】", ...args);
}

const { ccclass, property } = cc._decorator;
@ccclass
export class xxmLevelObjective extends cc.Component {
    /**等级 */
    private level: cc.Node = null;
    /**目标分数 */
    private targetScore: cc.Node = null;
    /**目标分动画 */
    private targetLayout: cc.Node = null;
    /**当前节点的 X 坐标 */
    private currentPosX: number = null;

    /**
     * @description: 则在所有组件的 update 之前调用。通常用于需要在所有组件的 onLoad 初始化完毕后执行的逻辑
     */
    protected start() {
        let buttons: cc.Button[] = this.node.getComponentsInChildren(cc.Button);
        buttons.forEach((button) => {
            // button.node.on('click', this.btnClickCallBack, this);
        });
    }


    /**
     * @description: onLoad 总是会在任何 start 方法调用前执行
     */
    protected onLoad(): void {
        this.level = cc.find('background/level', this.node);
        this.targetScore = cc.find('background/targetLayout/targetScore', this.node);
        this.targetLayout = cc.find('background/targetLayout', this.node);
        this.targetLayout.active = false;
        this.currentPosX = this.node.position.x;
        this.initData();
    }

    /**
     * @description: 关闭弹窗动效
     */
    public async showEffect() {

    }

    /**
     * @description: 初始化数据
     */
    private initData(): void {
        // 等级
        if (this.level) {
            let levelProgress: cc.Label = this.level.getComponent(cc.Label);
            let getLevel = LocalData.getItem(LocalCacheDataEnum.LEVEL);
            if (getLevel) {
                levelProgress.string = String(getLevel);
            } else {
                levelProgress.string = 1 + '';
                LocalData.setItem(LocalCacheDataEnum.LEVEL, 1);
            }
        }
        // 目标分
        if (this.targetScore) {
            let targetScore: cc.Label = this.targetScore.getComponent(cc.Label);
            let getLevel = LocalData.getItem(LocalCacheDataEnum.LEVEL);
            if (getLevel) {
                switch (getLevel) {
                    case LevelProgressEumn.ONE: {
                        targetScore.string = String(1000);
                        LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, 1000);
                        break;
                    }
                    case LevelProgressEumn.TWO: {
                        targetScore.string = String(2500);
                        LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, 2500);
                        break;
                    }
                    case LevelProgressEumn.THREE: {
                        targetScore.string = String(4000);
                        LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, 4000);
                        break;
                    }
                    case LevelProgressEumn.FOUR: {
                        targetScore.string = String(6000);
                        LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, 6000);
                        break;
                    }
                    case LevelProgressEumn.FIVE: {
                        targetScore.string = String(8000);
                        LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, 8000);
                        break;
                    }
                    default: {
                        let getScore = LocalData.getItem(LocalCacheDataEnum.TARGETSCORE);
                        if (getScore) {
                            let targetGrade: number = getScore + ((getLevel / 5) + 4) * 500;
                            targetScore.string = String(targetGrade);
                            LocalData.setItem(LocalCacheDataEnum.TARGETSCORE, targetGrade);
                        }
                        break;
                    }
                }
            }
        }
        // 执行入场动画和退场动画
        let winSize: number = cc.view.getVisibleSize().width;
        cc.tween(this.node)
            .to(0.5, { position: new cc.Vec3(winSize / 2, 0, 0) })
            .delay(1.5)
            .to(0.5, { position: new cc.Vec3(2 * winSize, 0, 0) })
            .call(() => {
                // 取消弹窗遮罩
                GTR.ui.close(xxmUIConfig.LevelScoreUI);
                // 刷新关卡界面
                GTR.event.dispatchEvent(xxmGameEvents.SHOWlEVEL);
                // 重新设置初始位置
                this.node.position = cc.v3(this.currentPosX, 0, 0);
            })
            .start();
        // 显示目标分数
        cc.tween(this.targetLayout)
            .delay(1)
            .to(0.1, { scale: cc.v3(2, 2, 0) })
            .set({ active: true })
            .to(0.1, { scale: cc.v3(1, 1, 0) })
            .start();
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    public onDestroy(): void {
    }
}

